import { defineStore } from 'pinia'
import type { EventStoreState } from '../type'

const useEventStore = defineStore('event', {
  state: (): EventStoreState => {
    return {
      time: '',
      timeList: [],
      hotWordType: '', // 当前热点关注选中的tab
      rankType: 0, // 当前领域、区域排名选中的tab
      wordType: 0 // 当前选中的热点词汇
    }
  },

  getters: {
    getTime: state => {
      return state.time
    }
  },

  actions: {
    setCurrentTime(time: string) {
      this.time = time
    },
    // 获取近一年月份
    timeSelectList() {
      const dataArr = []
      const data = new Date()
      const monthNum = 4 // 取到的月份

      data.setMonth(data.getMonth(), 1) // 获取到上个月月份,设置月份
      for (let i = 0; i < monthNum; i++) {
        data.setMonth(data.getMonth() - 1) // 每次循环一次 月份值减1
        const m = data.getMonth() + 1
        const y = data.getFullYear()
        const month = m < 10 ? '0' + m : m
        dataArr.push({
          name: y + '年' + m + '月',
          value: y + '-' + month + '-01 00:00:01'
        })
      }
      this.timeList = dataArr
    }
  }
})

export { useEventStore }
